<?php
date_default_timezone_set("PRC");
//define('IN_ECS', true);
header("Content-type: text/html; charset=utf-8");
header("Content-Length:31");
/*
require('../init.php');
*/
//|| count($_GET) > 1
if(empty($_GET) || empty($_GET['data']) ){
}else{
    $data = htmlspecialchars(trim($_GET['data']));
    $status = Ord_Verify($data);
    if($status != 200){
        echo $status;die;
    }else{
        $time = time();
        $row = $db->getOne("SELECT `id` FROM " . $ecs->table('users_medical_record') . " WHERE `Data`='".$data."'", true);
        $time = time();
        if(!$row){
            $array = array();
            $array['Start'] = substr($data,0,2);
            $array['VER'] = substr($data,2,2);
            $array['User'] = substr($data,4,1);
            $array['Customer'] = substr($data,5,2);
            $array['Model_Code'] = substr($data,7,2);
            $array['Device_Type'] = substr($data,9,2);
            $array['Serial_Number'] = substr($data,11,9);
            $array['Reserved1'] = substr($data,20,3);
            $array['Reserved2'] = substr($data,22,3);
            $array['Reserved3'] = substr($data,26,3);
            $array['DataTime'] = strtotime(substr(date('Y',$time),0,2).substr($data,29,2).'-'.substr($data,31,2).'-'.substr($data,33,2).' '.substr($data,35,2).':'.substr($data,37,2).':00');
            $array['Data'] = $data;
            $array['ctime'] = $time;
            $sql = "INSERT INTO " . $ecs->table('users_medical_record') . " (Start, VER, User, ".
                "Customer,Model_Code,Device_Type,Serial_Number,Reserved1,Reserved2,Reserved3,DataTime,Data,ctime)" .
                "VALUES ('$array[Start]','$array[VER]','$array[User]','$array[Customer]','$array[Model_Code]','$array[Device_Type]','$array[Serial_Number]','$array[Reserved1]','$array[Reserved2]','$array[Reserved3]','$array[DataTime]', '$array[Data]', '$array[ctime]' )";
            $res = $db->query($sql);
        }
        $ip1 = strtoupper(dechex(118));
        $ip2 = strtoupper(dechex(89));
        $ip3 = strtoupper(dechex(235));
        $ip4 = strtoupper(dechex(48));

        $post1 = strtoupper(dechex(80));
        $post2 = strtoupper(dechex(80));
        
        $sum1 = substr(dechex(intval($ip1,16)^intval($ip2,16)^intval($ip3,16)^intval($ip4,16)^intval($post1,16)^intval($post2,16)),0,2);
        $sum1 = strlen($sum1) < 2 ? '0'.strtoupper($sum1) : strtoupper($sum1);
       // $return='+IP'.$ip1.$ip2.$ip3.$ip4.$post1.$post2.'TTT'.$sum1;
       // echo $return;
        $returntime = date('Y-m-d H:i:s',$time);

        $y = strlen(dechex(substr($returntime,2,2))) < 2 ? '0'.strtoupper(dechex(substr($returntime,2,2))) : strtoupper(dechex(substr($returntime,2,2)));
        $m = strlen(dechex(substr($returntime,5,2))) < 2 ? '0'.strtoupper(dechex(substr($returntime,5,2))) : strtoupper(dechex(substr($returntime,5,2)));
        $d = strlen(dechex(substr($returntime,8,2))) < 2 ? '0'.strtoupper(dechex(substr($returntime,8,2))) : strtoupper(dechex(substr($returntime,8,2)));
        $h = strlen(dechex(substr($returntime,11,2))) < 2 ? '0'.strtoupper(dechex(substr($returntime,11,2))) : strtoupper(dechex(substr($returntime,11,2)));
        $i = strlen(dechex(substr($returntime,14,2))) < 2 ? '0'.strtoupper(dechex(substr($returntime,14,2))) : strtoupper(dechex(substr($returntime,14,2)));
        $sum2 = substr(dechex(intval($y,16)^intval($m,16)^intval($d,16)^intval($h,16)^intval($i,16)),0,2);
        $sum2 = strlen($sum2) < 2 ? '0'.strtoupper($sum2) : strtoupper($sum2);
        $return='+IP'.$ip1.$ip2.$ip3.$ip4.$post1.$post2.$sum1.$y.$m.$d.$h.$i.$sum2.'OK';
        $fp = fopen('php://output','a');
        fwrite($fp,$return);
        fclose($fp);
        //echo $return;
    }
}

function Ord_Verify($str = ''){
    $str = trim($str);
    if(empty($str)){
        return '10001';
    }
    $SUMDEC = 0;
    $Verify = '';
    for ($i=0; $i < 43; $i++) {
        if($i < 39){
            $SUMDEC += ord($str[$i]);
        }else{
            $Verify .= $str[$i];
        }
    }
    $SUMDEC = strtoupper(dechex($SUMDEC));
    if(empty($SUMDEC)){
        return '10002';
    }
    if(strlen($SUMDEC) < 4){
        if(strlen($SUMDEC) == 3){
            $SUMDEC = '0'.$SUMDEC;
        }elseif(strlen($SUMDEC) == 2){
            $SUMDEC = '00'.$SUMDEC;
        }else{
            $SUMDEC = '000'.$SUMDEC;
        }
    }
    if($SUMDEC == $Verify){
        return '200';
    }else{
        return '10003';
    }
}
?>
